package com.cy.listener;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JWindow;

import com.cy.controller.MasterController;

public class SyncMoveListener implements MouseListener,MouseMotionListener,WindowListener {
	private Point loc = null;
    private Point tmp = null;
    private boolean isDragged = false;
    
	@Override
	public void mouseClicked(MouseEvent e) {
	}
	@Override
	public void mouseEntered(MouseEvent e) {
	}
	@Override
	public void mouseExited(MouseEvent e) {
	}
	
	//实现棋盘和控制面板一起移动
	@Override
	public void mousePressed(MouseEvent e) {
		tmp = new Point(e.getX(), e.getY());
        isDragged = true;
	}
	//实现棋盘和控制面板一起移动
	@Override
	public void mouseReleased(MouseEvent e) {
		 isDragged = false;
	}
	/**
	 * 实现棋盘和控制面板一起移动
	 */
	@Override
	public void mouseDragged(MouseEvent e) {
		try {
			JFrame jFrame = (JFrame) e.getComponent();
			if(isDragged) {
				loc = new Point(jFrame.getLocation().x + e.getX() + jFrame.getWidth() - tmp.x,
						jFrame.getLocation().y + e.getY() - tmp.y);
				MasterController.getControlPanel().setLocation(loc);
			}
		} catch(Exception e1) {
			JWindow jWindow = (JWindow) e.getComponent();
			if(isDragged) {
				loc = new Point(jWindow.getLocation().x + e.getX() - MasterController.getChessboard().getWidth() - tmp.x,
						jWindow.getLocation().y + e.getY() - tmp.y);
				MasterController.getChessboard().setLocation(loc);
			}
		}
		finally {}
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		
	}
	@Override
	public void windowActivated(WindowEvent e) {
		MasterController.getControlPanel().setVisible(true);
	}
	@Override
	public void windowClosed(WindowEvent e) {
		
	}
	@Override
	public void windowClosing(WindowEvent e) {
		
	}
	@Override
	public void windowDeactivated(WindowEvent e) {
	}
	/**
	 * 实现了控制面板和棋盘的最大化同步
	 */
	@Override
	public void windowDeiconified(WindowEvent e) {
		MasterController.getControlPanel().setVisible(true);
	}
	/**
	 * 实现了控制面板和棋盘的最小化同步
	 */
	@Override
	public void windowIconified(WindowEvent e) {
		MasterController.getControlPanel().setVisible(false);
	}
	@Override
	public void windowOpened(WindowEvent e) {
	}

}
